package com.boc.image.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;
import com.boc.news.beans.EncryptMD5;

public class UserLogin {
	/**
	 * 用户登陆
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult userLogin(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			// 判断验证码是否正确
			Object checkCode = request.getSession().getAttribute("checkCode");
			if (checkCode == null) {
				result.addValue("PN_RET", "3");
				result.addValue("PC_MSG", "验证码已失效!");
				return result;
			}
			if (!checkCode.toString().toLowerCase().equals(m.get("cYzm").toString().toLowerCase())) {
	
				result.addValue("PN_RET", "3");
				result.addValue("PC_MSG", "验证码输入不正确!");
				return result;
			}
			String md5 = EncryptMD5.MD5(m.get("cLogin").toString() + m.get("cPass").toString());
			String ip = request.getLocalAddr();
			m.put("cIp", ip);
			m.put("cLoginpass", md5);
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userLogin", m);
			String flag = m.get("PN_RET").toString();
			if (!flag.equals("0")) {
				result.addValue("PN_RET", m.get("PN_RET"));
				result.addValue("PC_MSG", m.get("PC_MSG"));
			} else {	
				result.addValue("PN_RET", m.get("PN_RET"));
				result.addValue("PC_MSG", m.get("PC_MSG"));
//				result.addValue("PN_USERID", m.get("PN_USERID"));
//				result.addValue("PC_ZT", m.get("PC_ZT"));
				//把用户ID写入SESSION
				request.getSession().setAttribute("loginseccess", "1");
				request.getSession().setAttribute("userName", m.get("cLogin"));
				request.getSession().setAttribute("userId", m.get("PN_USERID"));
				request.getSession().setAttribute("userZt", m.get("PC_ZT"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 用户登陆
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult userLogin2(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			String md5 = EncryptMD5.MD5(m.get("cLogin").toString() + m.get("cPass").toString());
			String ip = request.getLocalAddr();
			m.put("cIp", ip);
			m.put("cLoginpass", md5);
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userLogin", m);
			String flag = m.get("PN_RET").toString();
			if (!flag.equals("0")) {
				result.addValue("PN_RET", m.get("PN_RET"));
				result.addValue("PC_MSG", m.get("PC_MSG"));
			} else {	
				result.addValue("PN_RET", m.get("PN_RET"));
				result.addValue("PC_MSG", m.get("PC_MSG"));
				//把用户ID写入SESSION
				request.getSession().setAttribute("loginseccess", "1");
				request.getSession().setAttribute("userName", m.get("cLogin"));
				request.getSession().setAttribute("userId", m.get("PN_USERID"));
				request.getSession().setAttribute("userZt", m.get("PC_ZT"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取得用户的基本信息
	 * @param userId
	 * @return
	 */
	public Map getUserInfo(Integer userId) {
		Map parameter = new HashMap();
		parameter.put("nUserid", userId);
		try {
			IbatisSqlClient.sqlMapClient.insert("image_procedure.userInfo", parameter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return parameter;
	}
	
	/**
	 * 取常见问题
	 * @return
	 */
	public List getFAQ() {
		List result = null;
		Map parameter = new HashMap();
		parameter.put("cZt", "1");
		try {
			result = IbatisSqlClient.sqlMapClient.queryForList("image_t_img_cjwt.ibatorgenerated_selectByPrimaryKey",parameter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 退出登陆
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult loginOut(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			request.getSession().removeAttribute("loginseccess");
			request.getSession().removeAttribute("userName");
			request.getSession().removeAttribute("userId");
			request.getSession().removeAttribute("userZt");
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
